package webInterface;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import db_manager.Manager;

/**
 * this class handles the login validation of a user
 */
public class LoginValidation extends HttpServlet
{
	/*
	 * this function handles the login validation of a user
	 */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        // getting the user name and password a user submitted
        String userName = request.getParameter("UserName").toString();
        String password = request.getParameter("Password").toString();
        Manager manager = Manager.getInstance();
        // checking if the user really exist
        Boolean b = manager.validateUser(userName, password);
        // the case that the user does exist
        if (b)
        {
        	HttpSession session = request.getSession();
        	session.setAttribute("userName", userName);
        	
        	//calling a new html file with the dirs and files of this user;
        	ArrayList<String> dirs;
        	ArrayList<String> files;
        	ArrayList<Long> dirsOfFiles = new ArrayList<Long>();
        	// getting this user dirs
        	dirs = manager.getDirsOfUser(userName);
        	// getting this user files;
        	files = manager.getFilesOfUser(dirs,userName, dirsOfFiles);
        	out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet Dirs and files</title>");
            out.println("</head>");
            out.println("<body>");
            out.println("<tr/>");
            out.println("Welcome "+userName);
            out.println("<br/>");
            out.println("Your directories:");
            out.println("<tr/>");
            out.println("<TABLE>");
            // for each dir of this user
            for (int i = 0; i < dirs.size(); i++) 
            {
            	// the case that this is not the dropbox dir
            	if (!dirs.get(i).equals("DropBox"))
            	{
            		out.println("	<form action=\"webInterface.ContentOfDir\" method=\"POST\"> \n <TD> <input type=\"submit\" name=\"act\" value="+dirs.get(i)+"> </TD> \n </form> \n");     	
            		out.println("<tr/>");
            	}
            }          
            out.println("</TABLE>");   

            out.println("Your files:");
            out.println("<tr/>");
            out.println("<TABLE>");
            String s;
            // for each file of the user
            for (int i = 0; i < files.size(); i++) 
            {
            	s = files.get(i) + "--dir_id:" + dirsOfFiles.get(i);
            	out.println("	<form action=\"webInterface.ContentOfDir\" method=\"POST\"> \n <TD> <input type=\"submit\" name=\"act2\" value="+ s +"> </TD> \n </form> \n");
                out.println("<tr/>");
			}
            out.println("</TABLE>");
            out.println("</body>");
            out.println("</html>");    
        }
        // the case that the user doesn't exist
        else
        {
        	// creating a "user doesn't exist" form
        	out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet Error</title>");
            out.println("</head>");
            out.println("<body>");
            out.println("	<form action=\"webInterface.Login\" method=\"GET\"> \n Wrong user name or password \n <br> <input type=\"submit\" value=\"Return to Login\"> \n </form> \n");
            out.println("</body>");
            out.println("</html>");          
        }
        out.close();
    }
}
